import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './filters/http-exception.filter';
import * as session from 'express-session';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // 设置全局前缀，与原系统保持兼容
  app.setGlobalPrefix('api');

  // 添加session支持
  app.use(
    session({
      secret: 'your-secret-key',
      resave: false,
      saveUninitialized: false,
      cookie: {
        maxAge: 86400000, // 24小时
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production', // 根据环境自动设置
        sameSite: 'lax' // 允许同站点携带Cookie
      }
    }),
  );

  // 允许跨域
  app.enableCors({
    origin: ['http://localhost:8085', 'http://localhost:3000'], // 明确列出允许的前端域名
    credentials: true, // 允许跨域请求携带Cookie
    exposedHeaders: ['set-cookie'] // 允许前端访问set-cookie头
  });

  // 注册全局异常过滤器
  app.useGlobalFilters(new HttpExceptionFilter());

  await app.listen(7001);
  console.log('Insight NestJS backend is running on port 7001');
}
bootstrap(); 